﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using DAO;
using DTO;
namespace BUS
{
    public class ThongKeBUS
    {
        public static DataTable ThongKeDaXN(string tungay, string denngay,string dviGui)
        {            
            DataTable dtKQ = new DataTable();
            if(dviGui =="")
                dtKQ = ThongKeDAO.ThongKeDaXN(tungay, denngay);
            else  
                dtKQ = ThongKeDAO.ThongKeDaXN(tungay, denngay, dviGui);
            if (dtKQ != null)
            {
                string dvCode="";
                string[] arrTen;
                string Ho="";
                int tongTB = 0, tongGPB =0;
                for (int i = 0; i < dtKQ.Rows.Count; i++)
                {                    
                    if (dtKQ.Rows[i]["idmau"].ToString() == "2")
                    {
                        tongGPB += 1;
                        dtKQ.Rows[i]["mau"] = "GPB";
                    }
                    else
                    {
                        tongTB += 1;
                        dtKQ.Rows[i]["mau"] = "TB";
                    }

                    if (dtKQ.Rows[i]["ksk"].ToString()=="1")
                    {
                        dtKQ.Rows[i]["donvicd"] = Uit.it_Font.VNIToUnicode(dtKQ.Rows[i]["donvicd"]);
                    }
                    dtKQ.Rows[i]["tongTB"] = tongTB;
                    dtKQ.Rows[i]["tongGPB"] = tongGPB;

                    arrTen = dtKQ.Rows[i]["HoTen"].ToString().Split(' ');
                    dtKQ.Rows[i]["Ten"] = arrTen[arrTen.Length - 1];
                    Ho = "";
                    for (int k = 0; k < arrTen.Length - 1; k++)
                    {
                        Ho += arrTen[k].ToString()+" ";
                    }
                    dtKQ.Rows[i]["HoTen"] = Ho;
                }
            }
            // sort table
            DataView dv = dtKQ.DefaultView;
            dv.Sort = "SoTieuBan ASC";
            DataTable data = new DataTable();
            data = dv.ToTable();
            int stt = 0;
            //danh so tt
            for (int i = 0; i < data.Rows.Count; i++)
            {
                stt += 1;
                data.Rows[i]["stt"] = stt;
            }
            return  data;
        }

        public static DataTable TimKetQua(string tungay, string denngay)
        {
            DataTable dtKQ = new DataTable();
            dtKQ = ThongKeDAO.TimKetQua(tungay, denngay);
            if (dtKQ != null)
            {
                for (int i = 0; i < dtKQ.Rows.Count; i++)
                {
                    if ((Boolean)dtKQ.Rows[i]["GioiTinh"])
                    {
                        dtKQ.Rows[i]["gioi"] = "Nam";
                    }
                    else
                    {
                        dtKQ.Rows[i]["gioi"] = "Nữ";
                    }
                    
                    int namsinh = Convert.ToInt32(dtKQ.Rows[i]["NamSinh"]);
                    dtKQ.Rows[i]["tuoi"] = DateTime.Today.Year - namsinh;
                    dtKQ.Rows[i]["nam"] = 
                        namsinh.ToString() + " (" + dtKQ.Rows[i]["tuoi"].ToString() + ")";

                    dtKQ.Rows[i]["KQDaiThe"] = Uit.it_Font.VNIToUnicode(dtKQ.Rows[i]["KQDaiThe"]);
                    if (dtKQ.Rows[i]["KQDaiThe"].ToString() == "NNull")
                        dtKQ.Rows[i]["KQDaiThe"] = "";
                    dtKQ.Rows[i]["KQViThe"] = Uit.it_Font.VNIToUnicode(dtKQ.Rows[i]["KQViThe"]);
                    if (dtKQ.Rows[i]["KQViThe"].ToString() == "NNull")
                        dtKQ.Rows[i]["KQViThe"] = "";
                    dtKQ.Rows[i]["KetLuan"] = Uit.it_Font.VNIToUnicode(dtKQ.Rows[i]["KetLuan"]);
                    if (dtKQ.Rows[i]["KetLuan"].ToString() == "NNull")
                        dtKQ.Rows[i]["KetLuan"] = "";
                    dtKQ.Rows[i]["tendvu"] = Uit.it_Font.VNIToUnicode(dtKQ.Rows[i]["tendvu"]);
                }
            }
            return dtKQ;
        }

        public static DataTable BC_ThuLaoCatLanh_KTV(string tungay, string denngay)
        {
            return ThongKeDAO.BC_ThuLaoCatLanh_KTV(tungay, denngay);
        }

        public static DataTable BC_ThuLaoCatLanh_BS(string tungay, string denngay)
        {
            return ThongKeDAO.BC_ThuLaoCatLanh_BS(tungay, denngay);
        }

        public static DataTable BC_ThuLao_TH(string tungay, string denngay
                                            , int LoaiDVu, int Loai)
        {
            DataTable dtRs = new DataTable();
            dtRs = ThongKeDAO.BC_ThuLao_TH(tungay, denngay, LoaiDVu, Loai);
            //int SlgTong = 0;
            if (dtRs == null)
                return dtRs;
            for (int i = 0; i < dtRs.Rows.Count; i++)
            {
                if (dtRs.Rows[i]["SlgNTru"].ToString() == "")
                    dtRs.Rows[i]["SlgNTru"] = 0;

                if (dtRs.Rows[i]["SlgNgTru"].ToString() == "")
                    dtRs.Rows[i]["SlgNgTru"] = 0;

                if (dtRs.Rows[i]["SlgCTtrinh"].ToString() == "")
                    dtRs.Rows[i]["SlgCTtrinh"] = 0;

                if (dtRs.Rows[i]["SlgKSK"].ToString() == "")
                    dtRs.Rows[i]["SlgKSK"] = 0;

                if (dtRs.Rows[i]["SlgMG"].ToString() == "")
                    dtRs.Rows[i]["SlgMG"] = 0;

                dtRs.Rows[i]["SlgTong"] = Convert.ToInt32(dtRs.Rows[i]["SlgNTru"])
                                          + Convert.ToInt32(dtRs.Rows[i]["SlgNgTru"])
                                          + Convert.ToInt32(dtRs.Rows[i]["SlgCTtrinh"])
                                          + Convert.ToInt32(dtRs.Rows[i]["SlgKSK"])
                                          + Convert.ToInt32(dtRs.Rows[i]["SlgMG"]);
               
            }

            dtRs.DefaultView.Sort = "SlgTong desc";
            dtRs = dtRs.DefaultView.ToTable();
            for (int i = 0; i < dtRs.Rows.Count; i++)
            {
                if (Convert.ToInt32(dtRs.Rows[i]["SlgTong"]) == 0)
                {
                    dtRs.Rows.RemoveAt(i);
                    i--;
                }
            }
            return dtRs;
        }

        public static DataTable BC_ThuLao_Liqui_TH(string tungay, string denngay)
        {
            DataTable dtRs = new DataTable();
            dtRs = ThongKeDAO.BC_ThuLao_Liqui_TH(tungay, denngay);
            if (dtRs == null)
            {
                return dtRs;
            }
            for (int i = 0; i < dtRs.Rows.Count; i++)
            {
                if (dtRs.Rows[i]["SlgNTru"].ToString() == "")
                    dtRs.Rows[i]["SlgNTru"] = 0;

                if (dtRs.Rows[i]["SlgNgTru"].ToString() == "")
                    dtRs.Rows[i]["SlgNgTru"] = 0;

                if (dtRs.Rows[i]["SlgCTtrinh"].ToString() == "")
                    dtRs.Rows[i]["SlgCTtrinh"] = 0;

                if (dtRs.Rows[i]["SlgKSK"].ToString() == "")
                    dtRs.Rows[i]["SlgKSK"] = 0;

                if (dtRs.Rows[i]["SlgMG"].ToString() == "")
                    dtRs.Rows[i]["SlgMG"] = 0;

                dtRs.Rows[i]["SlgTong"] = Convert.ToInt32(dtRs.Rows[i]["SlgNTru"])
                                          + Convert.ToInt32(dtRs.Rows[i]["SlgNgTru"])
                                          + Convert.ToInt32(dtRs.Rows[i]["SlgCTtrinh"])
                                          + Convert.ToInt32(dtRs.Rows[i]["SlgKSK"])
                                          + Convert.ToInt32(dtRs.Rows[i]["SlgMG"]);

            }

            dtRs.DefaultView.Sort = "SlgTong desc";
            dtRs = dtRs.DefaultView.ToTable();
            for (int i = 0; i < dtRs.Rows.Count; i++)
            {
                if (Convert.ToInt32(dtRs.Rows[i]["SlgTong"]) == 0)
                {
                    dtRs.Rows.RemoveAt(i);
                    i--;
                }
            }
            return dtRs;
        }

        public static DataTable BC_ThuLao_THXN(string tungay, string denngay)
        {
            DataTable dtRs = new DataTable();
            dtRs = ThongKeDAO.BC_ThuLao_THXN(tungay, denngay);
            int SlgTong = 0;

            for (int i = 0; i < dtRs.Rows.Count; i++)
            {
                if (dtRs.Rows[i]["SlgNTru"].ToString() == "")
                    dtRs.Rows[i]["SlgNTru"] = 0;

                if (dtRs.Rows[i]["SlgNgTru"].ToString() == "")
                    dtRs.Rows[i]["SlgNgTru"] = 0;

                if (dtRs.Rows[i]["SlgCTtrinh"].ToString() == "")
                    dtRs.Rows[i]["SlgCTtrinh"] = 0;

                if (dtRs.Rows[i]["SlgKSK"].ToString() == "")
                    dtRs.Rows[i]["SlgKSK"] = 0;

                if (dtRs.Rows[i]["SlgMG"].ToString() == "")
                    dtRs.Rows[i]["SlgMG"] = 0;

                dtRs.Rows[i]["SlgTong"] = Convert.ToInt32(dtRs.Rows[i]["SlgNTru"])
                                          + Convert.ToInt32(dtRs.Rows[i]["SlgNgTru"])
                                          + Convert.ToInt32(dtRs.Rows[i]["SlgCTtrinh"])
                                          + Convert.ToInt32(dtRs.Rows[i]["SlgKSK"])
                                          + Convert.ToInt32(dtRs.Rows[i]["SlgMG"]);

            }

            dtRs.DefaultView.Sort = "SlgTong desc";
            dtRs = dtRs.DefaultView.ToTable();
            for (int i = 0; i < dtRs.Rows.Count; i++)
            {
                if (Convert.ToInt32(dtRs.Rows[i]["SlgTong"]) == 0)
                {
                    dtRs.Rows.RemoveAt(i);
                    i--;
                }
            }
            return dtRs;
        }

        public static DataTable BC_ThuLao_CT(string tungay, string denngay)
        {
            DataTable dtRS = new DataTable();
           
            dtRS = ThongKeDAO.BC_ThuLao_CT(tungay, denngay);            

            return dtRS;
        }

        public static DataTable BC_ThuLao_Liqui_CT(string tungay, string denngay)
        {
            DataTable dtRS = new DataTable();

            dtRS = ThongKeDAO.BC_ThuLao_Liqui_CT(tungay, denngay);

            return dtRS;
        }

        public static DataTable BC_ThuLao_KQ_CT(string tungay, string denngay
                                                , int LoaiDVu, int LoaiBC)
        {
            DataTable dtRS = new DataTable();
          
            dtRS = ThongKeDAO.BC_ThuLao_KQ_CT(tungay, denngay, LoaiDVu, LoaiBC);
           
            return dtRS;
        }

        public static DataTable BC_ThuLao_XN_CT(string tungay, string denngay
                                                , int LoaiDVu, int LoaiBC)
        {
            DataTable dtRS = new DataTable();

            dtRS = ThongKeDAO.BC_ThuLao_XN_CT(tungay, denngay, LoaiDVu, LoaiBC);
           
            return dtRS;
        }
        
    }
}
